package com.hezhanying.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hezhanying.mapper.EmpMapper;
import com.hezhanying.pojo.Emp;
import com.hezhanying.pojo.PageBean;
import com.hezhanying.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean getAllEmp(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end){
        PageHelper.startPage(page, pageSize);
        List<Emp> empList = empMapper.getAllEmp(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) empList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }
    @Override
    public void addEmp(Emp emp){
        emp.setEntrydate(LocalDate.now());
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.addEmp(emp);
    }
    @Override
    public void deleteEmp(Integer id){
        empMapper.deleteEmp(id);
    }
    @Override
    public void updateEmp(Emp emp){
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
    }
    @Override
    public Emp login(Emp emp){
        Emp loginEmp = empMapper.getByUsernameAndPassword(emp);
        return loginEmp;
    }
}
